package com.mj.activity.user;

import org.json.JSONObject;

import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Toast;

import com.mj.MJApplaction;
import com.mj.R;
import com.mj.base.BaseActivity;
import com.mj.http.Url;

public class ChangePwdActivity extends BaseActivity {
	private String pwds, newpwdss;

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.activity_changepwd);
		init();
	}

	private void init() {
		// title
		aq.id(R.id.btn_left).clicked(this, "back");
		aq.id(R.id.btn_right).gone();
		aq.id(R.id.tv_title).text("修改密码");
		// bottom
		aq.id(R.id.button1).text("完成").clicked(this, "change");
	}

	private boolean check() {
		pwds = aq.id(R.id.editText1).getText().toString();
		if (TextUtils.isEmpty(pwds)) {
			aq.id(R.id.editText1).getView().requestFocus();
			Toast.makeText(this, "请输入原密码", Toast.LENGTH_LONG).show();
			return false;
		}
		newpwdss = aq.id(R.id.editText2).getText().toString();
		if (TextUtils.isEmpty(newpwdss)) {
			aq.id(R.id.editText2).getView().requestFocus();
			Toast.makeText(this, "请输入新密码", Toast.LENGTH_LONG).show();
			return false;
		}
		String repasswd = aq.id(R.id.editText3).getText().toString();
		if (TextUtils.isEmpty(repasswd)) {
			aq.id(R.id.editText3).getView().requestFocus();
			Toast.makeText(this, "请输入确认新密码", Toast.LENGTH_LONG).show();
			return false;
		}
		if (!newpwdss.equals(repasswd)) {
			aq.id(R.id.editText2).getView().requestFocus();
			Toast.makeText(this, "新密码与确认新密码不符", Toast.LENGTH_LONG).show();
			return false;
		}
		return true;
	}

	public void change() {
		if (check()) {
			baseCallBack callBack = new baseCallBack() {
				@Override
				public void onSuccess(JSONObject object) {
					super.onSuccess(object);
					Toast.makeText(ChangePwdActivity.this, "修改成功", Toast.LENGTH_LONG).show();
					finish();
				}
			};
			String userkey = "?userkey=" + MJApplaction.userInfo.getUserkey();
			pwds = "&pwds=" + pwds;
			newpwdss = "&newpwdss=" + newpwdss;
			String url = Url.resetpwd + userkey + pwds + newpwdss;
			getJson(url, callBack, true);
		}
	}
}
